//
//  ChineseSongController.m
//  Coco_Music
//
//  Created by 强哥 on 15/6/24.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "ChineseSongController.h"
#import "ChineseSongCell.h"
#import "AFN_block.h"
#import "MJRefresh.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefreshConst.h"

@interface ChineseSongController ()
@property(nonatomic,copy)NSString *stringID;
@property(nonatomic,retain)PlayingViewController *playController;



@end

@implementation ChineseSongController

-(void)dealloc
{
    [self.SongModle release];
    [self.allChinese release];
    [self.stringID release];
    [self.playController release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTable = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    self.myTable.dataSource = self;
    self.myTable.delegate = self;
    [self.view addSubview:self.myTable];
    [self.myTable release];
    self.allChinese = [NSMutableArray array];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中,请稍后";
    [self.HUD show:YES];

    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _allChinese.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellSpecia = @"cellSpecia";
    ChineseSongCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSpecia];
    if (cell == nil) {
        cell = [[ChineseSongCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSpecia];
    }
    cell.song = [_allChinese objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self add:_allChinese indexPath:indexPath.row];
    
}

-(void)setSongModle:(NewSong *)SongModle
{
    if (_SongModle != SongModle) {
        [_SongModle release];
        _SongModle = [SongModle retain];
    }
    if (SongModle == nil) {
    }
    else
    {
        NSString *string = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%@",SongModle.msg_id];
        [AFN_block NSString:string Blokc:^(id request) {
            NSMutableArray *array = [request objectForKey:@"data"];
            NSMutableDictionary *dic = [array firstObject];
            NSMutableArray *songArray = [dic objectForKey:@"songlist"];
            NSString *string1 = nil;
            NSString *string2 = nil;
            _stringID = nil;
            for (NSMutableDictionary *dic2 in songArray) {
                string1 = [NSString stringWithFormat:@"%@",[dic2 objectForKey:@"song_id"]];
                string2 = [NSString stringWithFormat:@"%@,%@",string1,string2];
                _stringID = [string2 substringToIndex:[string2 length]-7];
            }
            [AFN_block NSString:[NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",_stringID] Blokc:^(id request) {
                NSMutableArray *array = [request objectForKey:@"data"];
                
                for (NSMutableDictionary *dic in array) {
                    NewSong *song = [[NewSong alloc]init];
                    [song setValuesForKeysWithDictionary:dic];
                    [self.allChinese addObject:song];
                    [song release];
                }
                [self.myTable reloadData];
                [self.HUD hide:YES];
            }];
        }];
        
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 155;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
